home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / swtools / mipsABI / examples / before / ptysetup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  5.0 KB  |  232 lines

  1. /*
  2.  * mipsABI porting examples
  3.  *
  4.  * ptysetup - demonstrate the method for setting up pseduo-ttys
  5.  *
  6.  *    open_master():    open the master side of pty
  7.  *    open_slave():    open the slave side of the pty
  8.  *
  9.  */
  10. #define SGI4_PTY
  11.  
  12. #include <sys/types.h>
  13.  
  14. #ifdef HAVE_TERMIO
  15. #  include <termio.h>
  16. #else
  17. #  include <sgtty.h>
  18. #endif
  19.  
  20. #if defined(hpux) || (defined(sun) && defined(SVR4))
  21. #  include <sys/file.h>
  22. #endif
  23.  
  24. #ifdef SCO
  25. #  include <sys/fcntl.h>
  26. #endif
  27.  
  28. #ifdef STR_PTY
  29. #  include <sys/stropts.h>
  30. #  include <signal.h>
  31. #endif
  32.  
  33. #if defined(PTC_PTY)
  34. #  include <sys/stropts.h>
  35. #  include <sys/stat.h>
  36. #  include <sys/sysmacros.h>
  37. #elif defined(sgi)
  38. #  include <fcntl.h>
  39. #  include <signal.h>
  40. #  include <sys/stat.h>
  41. #endif
  42.  
  43. #include <stdio.h>
  44.  
  45. /* prototypes */
  46. static int open_master (void);
  47. static int open_slave (int);
  48.  
  49. #ifdef STR_PTY
  50. /* STREAMS ptys need separate read and write fd's */
  51. FILE *ex_fpin = NULL;
  52. FILE *ex_fpout = NULL;
  53. #else
  54. FILE *ex_fp = NULL;
  55. #endif
  56.  
  57. int ex_pid = 0;            /* example process id */
  58. static int ex_InputId;  /* example input id */
  59.  
  60. #if defined(PTC_PTY)
  61. static char pty[] = "/dev/ptc";         /* clone dev of pseudo-terminal */
  62. static char tty[15];                    /* slave side of pseudo-terminal */
  63. int pts_master;                         /* master filedes */
  64. char ex_tty[15];
  65. #endif
  66.  
  67. #if defined(IRIX4_PTY)
  68. char *_getpty(int*, int, mode_t, int);
  69. static char tty[15];
  70. static char pty[15];
  71. char ex_tty[15];
  72. #endif
  73.  
  74. #ifdef STR_PTY
  75. static char pty[] = "/dev/ptmx";
  76. static char tty[15];                    /* slave side of pseudo-terminal */
  77. int pts_master;                         /* master filedes */
  78. char ex_tty[15];
  79. #endif
  80.  
  81. #if !defined(STR_PTY) && !defined(PTC_PTY) && !defined(IRIX4_PTY)
  82. static char     pty[11] = "/dev/pty??";    /* master side of pseudo-terminal */
  83. static char     tty[11] = "/dev/tty??";    /* slave side of pseudo-terminal */
  84. char ex_tty[11] = "/dev/tty??";
  85. #endif
  86.  
  87. static int hold_slave = -1;
  88. extern char    *ex_prompt;
  89.  
  90. /*
  91.  * dummy main for compiling
  92.  */
  93. main ()
  94. {
  95.     open_master ();
  96. }
  97.  
  98. /*
  99.  *  This program talks to a child process through a pseudo terminal which
  100.  *  is a pair of master and slave devices: /dev/pty?? and /dev/tty??, where
  101.  *  ?? goes from p0 to sf (system dependent).  The pty is opened for both
  102.  *  read and write. Some systems use SYSV STREAMS based pty's. For these
  103.  *  define STR_PTY. Some use /dev/ptc based pty's, for those use PTC_PTY.
  104.  */
  105. static int open_master()
  106. {
  107.   int  i, master, fd;
  108.   char c;
  109.  
  110. #ifdef IRIX4_PTY
  111.   char *line;
  112.   SIG_PF oldintr;
  113. #endif
  114.   
  115. #if defined(PTC_PTY)
  116.   if ((pts_master = open(pty, O_RDWR)) >= 0)
  117.     {
  118.       struct stat sb;
  119.       
  120.       if (fstat(pts_master, &sb) < 0)
  121.     close(pts_master);
  122.       else
  123.     {
  124.       hold_slave = minor(sb.st_rdev);
  125.       return(pts_master);
  126.     }
  127.     }
  128. #endif
  129.  
  130. #ifdef IRIX4_PTY
  131.   if ((oldintr = signal(SIGCHLD, SIG_DFL)) == SIG_ERR)
  132.     {
  133.       perror("bad return from signal");
  134.       exit(1);
  135.     }
  136.   line = _getpty(&master, O_RDWR | O_NDELAY, 0600, 0);
  137.   if (signal(SIGCHLD, oldintr) == SIG_ERR)
  138.     {
  139.       perror("bad return from signal");
  140.       exit(1);
  141.     }
  142.   if(line)
  143.     {
  144.       strcpy(tty, line);
  145.       strcpy(ex_tty, line);
  146.       return(master);
  147.     }
  148. #endif
  149.  
  150. #ifdef STR_PTY
  151.   if ((pts_master = open(pty, O_RDWR)) >= 0)
  152.     {
  153.       grantpt(pts_master);
  154.       unlockpt(pts_master);
  155.       return(pts_master);
  156.     }
  157. #endif
  158.   
  159. #if !defined(STR_PTY) && !defined(PTC_PTY) && !defined(IRIX4_PTY)
  160.   for (c='p'; c<'z'; c++) 
  161.     {
  162.       for (i=0; i<16; i++) 
  163.     {
  164.       pty[8] = c;
  165.       pty[9] = "0123456789abcdef"[i];
  166.       tty[8] = c;
  167.       tty[9] = pty[9];
  168.       /*
  169.        * I need to check that tty is not the same device we are using
  170.        * for child's pseudo-terminal. Xtty has to find its own. If this
  171.        * it the first pseudo-tty then ex_tty[0] is "??" otherwise it
  172.        * keeps the last letters of child's tty.
  173.        */
  174.       if (strcmp(&ex_tty[8], &tty[8]) &&
  175.           (master = open(pty, O_RDWR)) >= 0)
  176.         {
  177.           if ((hold_slave = open(tty, O_RDWR)) >= 0)
  178.         return (master);
  179.           else close(master);
  180.         }
  181.     }
  182.     }
  183. #endif
  184.   
  185.   fprintf(stderr, "ptysetup: all ptys in use\n");
  186.   return(-1);
  187. }
  188.  
  189. static int open_slave(int pts_slave)
  190. {
  191.  
  192. #ifdef STR_PTY
  193.   char          *ptsname();
  194.   char          *slavename;
  195.  
  196.   slavename = ptsname (pts_master);    /* get name of slave */
  197.   strcpy (tty, slavename);        /* copy over to save area */
  198.   strcpy (ex_tty, slavename);
  199.   if ((pts_slave = open(tty, O_RDWR)) >= 0)
  200.     {
  201.       /*XXX todo: need to make sure these succeed */
  202.       ioctl (pts_slave, I_PUSH, "ptem");
  203.       ioctl (pts_slave, I_PUSH, "ldterm");
  204.       ioctl (pts_slave, I_PUSH, "ttcompat");
  205.       return(pts_slave);
  206.     }
  207. #endif
  208.  
  209. #if defined(PTC_PTY)
  210.   sprintf(tty, "/dev/ttyq%d", hold_slave);
  211.   if ((pts_slave = open(tty, O_RDWR)) >= 0) {
  212.     strcpy(ex_tty, tty);
  213.     return(pts_slave);
  214.   }
  215. #endif
  216.  
  217. #ifdef IRIX4_PTY
  218.   if ((pts_slave = open(tty, O_RDWR)) >= 0) {
  219.     strcpy(ex_tty, tty);
  220.     return(pts_slave);
  221.   }
  222. #endif
  223.  
  224. #if !defined(STR_PTY) && !defined(PTC_PTY) && !defined(IRIX4_PTY)
  225.   return(hold_slave);
  226. #else
  227.   fprintf(stderr, "ptysetup: failed to open slave pty\n");
  228.   return(-1);
  229. #endif
  230.  
  231. }
  232.